package models

import (
	"errors"
	"time"

	"gorm.io/gorm"
)

type Menu struct {
	MenuId        string    `json:"id" gorm:"column:id;primaryKey;type:varchar(64);comment:MenuId"`
	ParentId      string    `json:"parentId" gorm:"type:varchar(64);comment:ParentId"`
	MenuName      string    `json:"menuName" gorm:"type:varchar(50);comment:MenuName"`
	Path          string    `json:"path" gorm:"type:varchar(255);comment:Path"`
	Component     string    `json:"component" gorm:"type:varchar(255);comment:Component"`
	Perms         string    `json:"perms" gorm:"type:text;comment:Perms"`
	Icon          string    `json:"icon" gorm:"type:varchar(255);comment:Icon"`
	Type          string    `json:"type" gorm:"type:varchar(50);comment:Type"`
	OrderNum      int32     `json:"orderNum" gorm:"type:double;comment:OrderNum"`
	CreateTimeDts time.Time `json:"-" gorm:"type:datetime;column:create_time;comment:创建时间"`
	CreateTime    string    `json:"createTime" gorm:"-"`
	ModifyTimeDts time.Time `json:"-" gorm:"type:datetime;column:update_time;comment:UpdateTime"`
	ModifyTime    string    `json:"modifyTime" gorm:"column:update_time;"`
	CreatorId     string    `json:"creatorId" gorm:"type:varchar(64);comment:CreatorId"`
	Creator       string    `json:"creator" gorm:"type:varchar(64);comment:Creator"`
	Title         string    `json:"title" gorm:"type:varchar(64);comment:Title"`
	BtnClass      string    `json:"btnClass" gorm:"type:varchar(50);comment:BtnClass"`
	BtnGroup      string    `json:"btnGroup" gorm:"type:varchar(50);comment:BtnGroup"`
	Enable        string    `json:"enable" gorm:"type:varchar(50);comment:Enable"`
	Lang          string    `json:"lang" gorm:"type:varchar(50);comment:Lang"`
	Systemid      string    `json:"systemid" gorm:"type:varchar(64);comment:SystemId"`
	Children      []Menu    `json:"children,omitempty" gorm:"-"`
	Alias         string    `json:"alias" gorm:"-"`
}

func (Menu) Add(db *gorm.DB, data interface{}) (err error) {
	err = db.Model(&Menu{}).Create(data).Error
	return err
}

func (Menu) Mod(db *gorm.DB, data *Menu, condition ...func(db *gorm.DB) *gorm.DB) (err error) {
	tx := db.Model(&Menu{}).
		Where("id = ?", data.MenuId).
		//Scopes(condition...).
		Updates(data)
	if tx.RowsAffected == 0 {
		return errors.New("修改失败")
	}
	return tx.Error
}

func (Menu) ModByModel(db *gorm.DB, data Menu) (err error) {
	tx := db.Model(&Menu{}).Where("id = ?", data.MenuId).Updates(data)
	if tx.RowsAffected == 0 {
		return errors.New("修改失败")
	}
	return tx.Error
}

func (Menu) FindOne(db *gorm.DB, data interface{}, condition ...func(db *gorm.DB) *gorm.DB) (err error) {
	err = db.Model(&Menu{}).
		Scopes(
			condition...,
		).First(data).Error
	return err
}

func (Menu) FindList(db *gorm.DB, data interface{}, condition ...func(db *gorm.DB) *gorm.DB) (err error) {
	err = db.Model(&Menu{}).Scopes(
		condition...,
	).Order("order_num asc").Find(data).Limit(-1).Offset(-1).Error
	return err
}

func (Menu) FindPage(db *gorm.DB, data interface{}, count *int64, condition ...func(db *gorm.DB) *gorm.DB) (err error) {
	err = db.Model(&Menu{}).Scopes(
		condition...,
	).Find(data).Limit(-1).Offset(-1).Count(count).Error
	return err
}

func (Menu) Del(db *gorm.DB, conds string, params ...interface{}) (err error) {
	tx := db.Model(&Menu{}).Where(conds, params...).Delete(&Menu{})
	if tx.RowsAffected == 0 {
		return errors.New("删除失败")
	}
	return tx.Error
}

func (Menu) FindCountByConds(db *gorm.DB, count *int64, conds string, params ...interface{}) {
	db.Model(&Menu{}).Where(conds, params...).Find(&Menu{}).Count(count)
	return
}
func (Menu) FindListByConds(db *gorm.DB, data interface{}, conds string, params ...interface{}) (err error) {
	db.Model(&Menu{}).Where(conds, params...).Find(data)
	return
}

func (Menu) TableName() string {
	return "sys_menu"
}

func (e *Menu) Generate() interface{} {
	o := *e
	return &o
}
